#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/poll.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <errno.h>


int main(void)
{
	int file;
	int addr=0x65; //1100101
	//struct i2c_msg i2c_mess;
	struct i2c_smbus_ioctl_data i2c_ioctl;
	union i2c_smbus_data datas;

	datas.byte=0;

	unsigned long func=0;
	unsigned long pec=0;

	int err=0;

	if( (file = open("/dev/i2c-3",O_RDWR)) < 0)
	{
		printf("Error \n");
		exit(1);
	}

	printf("%d %d\n",ioctl(file,I2C_FUNCS,&func), ioctl(file,I2C_PEC,&pec));

	if( ioctl(file,I2C_SLAVE_FORCE,addr) < 0)
	{
		printf("error : I2C Address can't open \n");
		exit(1);
	}

	printf("funcs code : %x PEC %x\n",func,pec);

	datas.block[0]=1;
	i2c_ioctl.read_write=I2C_SMBUS_READ;
	i2c_ioctl.command=0x00|0x80;
	i2c_ioctl.size=I2C_SMBUS_BYTE_DATA;
	i2c_ioctl.data=&datas;

	err=ioctl(file, I2C_SMBUS, &i2c_ioctl);

	printf("er %d %d %s\n",err,datas.byte,strerror(errno));

	i2c_ioctl.read_write=I2C_SMBUS_WRITE;
	i2c_ioctl.command=0x05|0x80;
	i2c_ioctl.size=I2C_SMBUS_BYTE_DATA;
	i2c_ioctl.data=&datas;

	err=ioctl(file, I2C_SMBUS, &i2c_ioctl);

	printf("er %d %d %s\n",err,datas.byte,strerror(errno));

	close(file);

}
